<?php
/*
 +----------------------------------------------------------------------
 + Title        : ArticleCateLogic 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/2/28 22:01
 + Last-time    : 2021/2/28 22:01+ chen2
 + Desc         : ArticleCateLogic
 +----------------------------------------------------------------------
*/


namespace app\common\service;


use app\common\model\ArticleCate;
use think\Db;

class ArticleCateService {

	/**
	 *
	 * @param $params
	 *
	 * @return \think\Paginator
	 * @throws \think\exception\DbException
	 * @author : Randy_chen
	 * @Date   : 2021/3/2
	 * @Time   : 22:39
	 */
	public function getArticleCateList($params) {
		$list = ArticleCate::build()
		                   ->equal('cate_type,parent_id')
		                   ->where('is_deleted', 0)
		                   ->order('create_time desc')
		                   ->_paginate();
		return $list;
	}

	/**
	 *
	 * @param $id
	 *
	 * @return array|bool|\PDOStatement|string|\think\Model|null
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * @author : Randy_chen
	 * @Date   : 2021/3/2
	 * @Time   : 22:39
	 */
	public function getArticleCateItem($id) {
		$item = ArticleCate::where('article_cat_id', $id)
		                   ->findOrFail();
		return $item;
	}

	/**
	 *
	 * @author : Randy_chen
	 * @Date   : 2021/3/2
	 * @Time   : 22:39
	 */
	public function addArticleCateItem($data) {
		Db::startTrans();
		try {
			ArticleCate::build()
			           ->allowField(true)
			           ->save($data);//添加用户装修休息
			Db::commit();
			return true;
		} catch (\Exception $e) {
			Db::rollback();
			throw $e;
		}
	}


	/**
	 *
	 * @param $params
	 *
	 * @return bool
	 * @author : Randy_chen
	 * @Date   : 2021/3/2
	 * @Time   : 22:40
	 */
	public function setArticleCateItem($data) : bool {
		Db::startTrans();
		try {
			ArticleCate::build()
			           ->allowField(true)
			           ->save($data, ['article_cat_id' => $data['id']]);
			Db::commit();
			return true;
		} catch (\Exception $e) {
			Db::rollback();
			throw $e;
		}
	}

	/**
	 *
	 * @author : Randy_chen
	 * @Date   : 2021/3/2
	 * @Time   : 22:40
	 */
	public function delArticleCateItem(string $id) : bool {

		ArticleCate::build()
		           ->where('article_cat_id', $id)
		           ->setField('is_deleted', 1);
		return true;

	}

}